<template>
  <div class="app-container">
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">{{
          scope.$index + (currenPage - 1) * eachPage + 1
        }}</template>
      </el-table-column>
      <el-table-column prop="title" label="文章名称" width="180" align="center">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            title="博客预览图"
            width="230"
            trigger="hover"
          >
            <el-image
              style="width: 200px; height: 200px"
              :src="scope.row.thumb"
              fit="contain"
              :preview-src-list="srcList"
            ></el-image>
            <a
              href="#"
              target="_blank"
              @click.prevent="goToTitltHandle(scope.row)"
              slot="reference"
              >{{ scope.row.title }}</a
            >
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="文章描述">
        <template slot-scope="scope">{{ scope.row.description }}</template>
      </el-table-column>
      <el-table-column
        prop="description"
        label="浏览数"
        width="80"
        align="center"
      >
        <template slot-scope="scope">{{ scope.row.scanNumber }}</template>
      </el-table-column>
      <el-table-column
        prop="description"
        label="评论量"
        width="80"
        align="center"
      >
        <template slot-scope="scope">{{ scope.row.commentNumber }}</template>
      </el-table-column>
      <el-table-column
        prop="description"
        label="所属分类"
        width="180"
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.blogtype === null ? '未分类' : scope.row.blogtype.name
        }}</template>
      </el-table-column>
      <el-table-column prop="description" label="创建日期" width="250">
        <template slot-scope="scope">{{ scope.row.createDate }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="editBloghandle(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="deleteBlogHandle(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      style="margin-top: 30px"
      background
      layout="prev, pager, next, total, ->, sizes, jumper"
      :total="count"
      :page-size="eachPage"
      :page-sizes="[3, 10, 20, 30]"
      :current-page.sync="pageCurrentPage"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      @prev-click="prevClickHandle"
      @next-click="nextClickHandle"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getBlog, deleteBlog } from "@/api/blog.js";
import { formatDate } from "@/utils/tools.js";
import url from "@/urlConfig.js";
export default {
  data() {
    return {
      tableData: [],
      srcList: [],
      eachPage: 3, // 每一页显示的条数
      currenPage: 1, // 当前页码，默认第一页
      totalPage: 0, // 总页数
      count: 0, // 数据总数
      pageCurrentPage: 1, // 分页当前页码
    };
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getBlog(this.currenPage, this.eachPage).then(({ data }) => {
        this.tableData = data ? data.rows : [];
        for (const item of this.tableData) {
          item.createDate = formatDate(item.createDate);
          item.thumb = url.server_URL + item.thumb;
          this.srcList.push(item.thumb);
        }
        
        this.count = data.total;
        this.totalPage = Math.ceil(this.count / this.eachPage);
        if (this.currenPage > this.totalPage && this.count > 0) {
          this.currenPage = this.totalPage;
          this.fetchData();
        }
      });
    },
    // 跳转到具体文章
    goToTitltHandle(blogInfo) {
      window.open(`${url.frontEnd_URl}/article/${blogInfo.id}`);
    },
    // 删除
    deleteBlogHandle(blogInfo) {
      this.$confirm(
        "删除该文章会将文章下面的评论一并删除, 是否继续?",
        "是否删除此篇文章",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deleteBlog(blogInfo.id).then(() => {
            this.fetchData();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页相关设置
    sizeChangeHandle(pagerNubmer) {
      this.eachPage = parseInt(pagerNubmer);
      this.currenPage = 1;
      this.pageCurrentPage = 1;
      this.fetchData();
    },
    currentChangeHandle(pagerNubmer) {
      this.currenPage = parseInt(pagerNubmer);
      this.fetchData();
    },
    prevClickHandle() {
      this.currenPage -= 1;
    },
    nextClickHandle() {
      this.currenPage += 1;
    },

    // 修改文章数据
    editBloghandle(blogInfo) {
      this.$router.push(`/editBlog/${blogInfo.id}`)
    }
  },
};
</script>

<style>
</style>
